package com.yyf.test.Controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class FileController {

    @PostMapping("/save-file")
    public Map<String, Object> saveFile(@RequestBody Map<String, String> request) {
        Map<String, Object> response = new HashMap<>();
        String filename = request.get("filename");
        String content = request.get("content");

        if (filename != null && content != null) {
            try {
//                                                             改成自己的地址↓
                File file = new File("D:/httpsTest/src/main/resources/txt/" + filename);
                if (!file.exists()) {
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                }

                FileWriter writer = new FileWriter(file);
                writer.write(content);
                writer.close();

                response.put("success", true);
            } catch (IOException e) {
                e.printStackTrace();
                response.put("success", false);
            }
        } else {
            response.put("success", false);
        }

        return response;
    }
}

